<?php

namespace App\Console\Commands;

use App\Model\Device;
use App\Model\InverterData;
use App\Model\InverterDataThree;
use App\Model\InverterDataTwo;
use App\Model\Site;
use App\Model\StatisticsDeviceYearEnergy;
use App\Model\StatisticsDeviceYearEnergyCopy;
use Illuminate\Console\Command;

class StatisticYear extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'StatisticYear';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '统计每年';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
//        $begin_time = date('Y-m-d') . ' 0:0:0';
        $begin_time = '2020-12-31 00:00:00';
//        $end_time = date('Y-m-d') . ' 23:59:59';
        $end_time = '2020-12-31 23:59:59';
        $devices = Device::all();
        foreach ($devices  as $device) {
//            if($device->site_id < 18) {
//                $data = InverterData::where('device_id', $device->id)
//                    ->where('site_id',$device->site_id)
//                    ->whereBetween('EventTime', [$begin_time, $end_time])
//                    ->orderBy('id', 'desc')->first();
//            }else{
//                $data = InverterDataTwo::where('device_id', $device->id)
//                    ->where('site_id',$device->site_id)
//                    ->whereBetween('EventTime', [$begin_time, $end_time])
//                    ->orderBy('id', 'desc')->first();
//            }
            $site_id = $device->site_id;
            switch ($site_id){
                case $site_id < 18:
                    $data = InverterData::where('device_id', $device->device_id)
                    ->where('site_id',$device->site_id)
                    ->whereBetween('EventTime', [$begin_time, $end_time])
                    ->orderBy('id', 'desc')->first();
                    break;
                case $site_id>=18&&$site_id<43:
                    $data = InverterDataTwo::where('device_id', $device->device_id)
                    ->where('site_id',$device->site_id)
                    ->whereBetween('EventTime', [$begin_time, $end_time])
                    ->orderBy('id', 'desc')->first();
                    break;
                default:
                    $data = InverterDataThree::where('device_id', $device->device_id)
                        ->where('site_id',$device->site_id)
                        ->whereBetween('EventTime', [$begin_time, $end_time])
                        ->orderBy('id', 'desc')->first();
//                    break;
//                default:
//                    $data = InverterData::where('device_id', $device->id)
//                        ->where('site_id',$device->site_id)
//                        ->whereBetween('EventTime', [$begin_time, $end_time])
//                        ->orderBy('id', 'desc')->first();
            }


            if($data) {
                $obj = StatisticsDeviceYearEnergy::where('site_id',$device->site_id)->where('device_id',$device->device_id)->where('date','2020')->first();
                if(!$obj) {
                    StatisticsDeviceYearEnergy::create(['site_id' => $device->site_id, 'user_id' => $device->user_id, 'device_id' => $device->device_id, 'value' => $data->TotGenCap, 'date' => '2020']);
                }
            }
        }
    }
}
